package com.ilarele.shopping.client.adaptors;

import java.util.Comparator;

import com.ilarele.shopping.Product;
import com.ilarele.shopping.ProductLocation;

public class Sorter {
	public static final int SORT_DIR_ASC = 0;
	public static final int SORT_DIR_DESC = 1;
	public static final int SORT_DIR_RANDOM = 2;

	public static final int SORT_CRIT_DATE = 0;
	public static final int SORT_CRIT_LOCATION = 1;
	public static final int SORT_CRIT_PRICE = 2;

	private static ProductComparatorLocation compLocation;
	private static final ProductComparatorDate compDate = new ProductComparatorDate();
	private static final ProductComparatorPrice compPrice = new ProductComparatorPrice();

	private int sortDirection = SORT_DIR_ASC;
	private int sortCriteria = SORT_CRIT_PRICE;

	public Sorter(ProductLocation fixPoint) {
		compLocation = new ProductComparatorLocation(fixPoint);
	}

	public void setSortDirection(int dir) {
		sortDirection = dir;
	}

	public void setSortCriteria(int criteria) {
		sortCriteria = criteria;
	}

	public Comparator<Product> getCurrentComparator() {
		switch (sortCriteria) {
		case SORT_CRIT_DATE:
			compDate.setDirection(sortDirection);
			return compDate;
		case SORT_CRIT_LOCATION:
			compLocation.setDirection(sortDirection);
			return compLocation;
		case SORT_CRIT_PRICE:
			compPrice.setDirection(sortDirection);
			return compPrice;
		}
		return compDate;
	}
}
